package com.zhuhjay.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuhjay.ruiji.common.CustomException;
import com.zhuhjay.ruiji.domain.Setmeal;
import com.zhuhjay.ruiji.domain.SetmealDish;
import com.zhuhjay.ruiji.domain.dto.SetmealDto;
import com.zhuhjay.ruiji.mapper.SetmealMapper;
import com.zhuhjay.ruiji.service.ICategoryService;
import com.zhuhjay.ruiji.service.ISetmealDishService;
import com.zhuhjay.ruiji.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-05-15
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSetmealAndDish(SetmealDto setmealDto) {
        // 添加套餐信息
        this.save(setmealDto);

        // 关联dish菜品表
        setmealDto.getSetmealDishes().forEach(item -> item.setSetmealId(setmealDto.getId().toString()));
        // 保存所有的关联表
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public SetmealDto querySetmaelAndDish(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        // 数据拷贝
        BeanUtils.copyProperties(setmeal, setmealDto);
        // 查询该套餐的所有菜品
        setmealDto.setSetmealDishes(setmealDishService.list(new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, setmealDto.getId().toString())));
        // 查询该套餐的菜品分类
        setmealDto.setCategoryName(categoryService.getById(setmealDto.getCategoryId()).getName());
        return setmealDto;
    }

    @Override
    public void updateSetmealAndDish(SetmealDto setmealDto) {
        // 更新套餐信息
        this.updateById(setmealDto);

        // 删除所有关联的表
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, setmealDto.getId()));
        // 重新关联dish菜品表
        setmealDto.getSetmealDishes().forEach(item -> item.setSetmealId(setmealDto.getId().toString()));
        // 保存所有的关联表
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmealAndDish(List<Long> ids) {
        // 停售状态的套餐才能够删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 查询是否有起售状态的套餐
        queryWrapper.in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);
        if( this.count(queryWrapper) > 0 ){
            throw new CustomException("套餐正在售卖中, 不能删除");
        }
        // 删除套餐表
        this.removeByIds(ids);

        // 删除关联表
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }
}
